package com.power.web.action.entrust;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;

import com.power.Constants;
import com.power.model.entrust.RzSpeedyEntrust;
import com.power.services.entrust.RzSpeedyEntrustService;
import com.power.util.DateJsonValueProcessor;
import com.power.util.DateTimeUtil;
import com.power.util.RequestUtil;
import com.power.web.action.base.AbstractBaseAction;

public class RzSpeedyEntrustAction extends AbstractBaseAction {

	private RzSpeedyEntrust obj;
	private File imgFile;
	private String imgFileFileName;
	private String jsonString;

	@Autowired
	private RzSpeedyEntrustService service;
	
	public String save() {
			if (null != obj) {
				obj.setInputTime(DateTimeUtil.getCurrentDateTime());
				obj.setIsDone("1");
			}
			this.response.reset();
			response.setContentType("text/html;charset=utf-8");
			try {
				service.create(obj);
			} catch (Exception e) {
				e.printStackTrace();
			}
		return null;
	}
	public String buildJSONString(){
		Map<String,String> map = paramMap();
		String callback = this.callback;
		int start = this.start;
		int limit = this.limit;
		if (limit == 0) {
			limit = 20;
		}
		List<RzSpeedyEntrust> list = null;
		StringBuffer _JSONStr = new StringBuffer();
		try {
			list = service.findALLbyParam(map,start,limit);
			_JSONStr.append(callback).append("({\"total\":").append(list.size())
					.append(",\"results\":");
			JsonConfig cfg = new JsonConfig();
			cfg.registerJsonValueProcessor(java.util.Date.class,
					new DateJsonValueProcessor("yyyy-MM-dd"));
			cfg.registerJsonValueProcessor(java.sql.Date.class,
					new DateJsonValueProcessor("yyyy-MM-dd"));
			JSONArray jsonObject = JSONArray.fromObject(list, cfg);
			_JSONStr.append(jsonObject.toString());
			_JSONStr.append("})");
		} catch (Exception e) {
			jsonString = "[]";
		}
		jsonString = _JSONStr.toString();
		return Constants.JSONSTRING_RESULT_NAME;
	}
	
	private Map<String,String> paramMap(){
		Map<String,String> map = new HashMap<String,String>();
		String orgId = RequestUtil.getParam(request, "orgId", "");
		String checkStatus = RequestUtil.getParam(request, "checkStatus", "");
		String paperTitle = RequestUtil.getParam(request, "paperTitle", "");
		String paperTimeBeginDate = RequestUtil.getParam(request, "paperTimeBeginDate", "");
		String paperTimeEndDate = RequestUtil.getParam(request, "paperTimeEndDate", "");
		String queryLocalFlag = RequestUtil.getParam(request, "queryLocalFlag", "");
		String yearComboStr = RequestUtil.getParam(request, "yearComboStr", "");
		map.put("orgId", orgId);
		map.put("queryLocalFlag", queryLocalFlag);
		map.put("checkStatus", checkStatus);
		map.put("paperTitle", paperTitle);
		map.put("paperTimeBeginDate", paperTimeBeginDate);
		map.put("paperTimeEndDate", paperTimeEndDate);
		map.put("forAudit", RequestUtil.getParam(request, "forAudit", "false"));
		map.put("gbLevel", RequestUtil.getParam(request, "gbLevel", ""));
		map.put("yearComboStr", yearComboStr);

		return map;
	}
	//删除
	  public String delete() {
		String id = RequestUtil.getParam(this.request, "deleteID", "");
		String[] ids = id.split("_");
		try {
			this.response.setCharacterEncoding("UTF-8");
			for (int i = 0; i < ids.length; i++) {
				RzSpeedyEntrust group = service.read(new Integer(ids[i]));
				service.delete(group);
			}
			this.response.getWriter().print("{success:true,msg:'删除成功!'}");
		} catch (Exception e) {
			log.error("输出JSON数据错误！", e);
			try {
				this.response.getWriter().print("{failure:true,msg:'删除失败!'}");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}
//有价值
	  public String onTui() {
			String id = RequestUtil.getParam(this.request, "deleteID", "");
			String[] ids = id.split("_");
			try {
				this.response.setCharacterEncoding("UTF-8");
				for (int i = 0; i < ids.length; i++) {
					RzSpeedyEntrust group = service.read(new Integer(ids[i]));
					group.setIsUse("0");
					service.update(group);
				}
				this.response.getWriter().print("{success:true,msg:'操作成功!'}");
			} catch (Exception e) {
				log.error("输出JSON数据错误！", e);
				try {
					this.response.getWriter().print("{failure:true,msg:'操作失败!'}");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			return null;
		}
	  //没价值
	  public String onNoTui() {
			String id = RequestUtil.getParam(this.request, "deleteID", "");
			String[] ids = id.split("_");
			try {
				this.response.setCharacterEncoding("UTF-8");
				for (int i = 0; i < ids.length; i++) {
					RzSpeedyEntrust group = service.read(new Integer(ids[i]));
					group.setIsUse("1");
					service.update(group);
				}
				this.response.getWriter().print("{success:true,msg:'操作成功!'}");
			} catch (Exception e) {
				log.error("输出JSON数据错误！", e);
				try {
					this.response.getWriter().print("{failure:true,msg:'操作失败!'}");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			return null;
		}
		//储备
	  public String onHot() {
			String id = RequestUtil.getParam(this.request, "deleteID", "");
			String[] ids = id.split("_");
			try {
				this.response.setCharacterEncoding("UTF-8");
				for (int i = 0; i < ids.length; i++) {
					RzSpeedyEntrust group = service.read(new Integer(ids[i]));
					group.setIsSave("0");
					service.update(group);
				}
				this.response.getWriter().print("{success:true,msg:'操作成功!'}");
			} catch (Exception e) {
				log.error("输出JSON数据错误！", e);
				try {
					this.response.getWriter().print("{failure:true,msg:'操作失败!'}");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			return null;
		}
	//取消储备
	  public String onNoHot() {
			String id = RequestUtil.getParam(this.request, "deleteID", "");
			String[] ids = id.split("_");
			try {
				this.response.setCharacterEncoding("UTF-8");
				for (int i = 0; i < ids.length; i++) {
					RzSpeedyEntrust group = service.read(new Integer(ids[i]));
					group.setIsSave("1");
					service.update(group);
				}
				this.response.getWriter().print("{success:true,msg:'操作成功!'}");
			} catch (Exception e) {
				log.error("输出JSON数据错误！", e);
				try {
					this.response.getWriter().print("{failure:true,msg:'操作失败!'}");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			return null;
		}
//是否处理，处理意见
	  public String isDone() {
			
			try {
				this.response.setCharacterEncoding("UTF-8");
					RzSpeedyEntrust group = service.read(new Integer(obj.getId()));
					group.setIsDone("0");
					group.setDoneIdea(obj.getDoneIdea());
					group.setDoneTime(DateTimeUtil.getCurrentDateTime());
					service.update(group);
				this.response.getWriter().print("{success:true,msg:'操作成功!'}");
			} catch (Exception e) {
				log.error("输出JSON数据错误！", e);
				try {
					this.response.getWriter().print("{failure:true,msg:'操作失败!'}");
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			return null;
		}  
	public RzSpeedyEntrust getObj() {
		return obj;
	}

	public void setObj(RzSpeedyEntrust obj) {
		this.obj = obj;
	}

	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public String getImgFileFileName() {
		return imgFileFileName;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	public String getJsonString() {
		return jsonString;
	}

	public void setJsonString(String jsonString) {
		this.jsonString = jsonString;
	}
	
}
